var ytVideoApp = {};
ytVideoApp.MAX_RESULTS_LIST = 5;
ytVideoApp.NAVIGATION_DIV = 'navigationForm';
ytVideoApp.VIDEO_LIST_CONTAINER_DIV = 'searchResultsVideoList';
ytVideoApp.VIDEO_SEARCH_RESULTS_DIV = 'searchResultsVideoColumn';
ytVideoApp.VIDEO_GRID_DIV = 'container';
ytVideoApp.VIDEO_TEXTAREA_DIV='share_feed_area';
ytVideoApp.VIDEO_PLAYER_DIV = 'videoPlayer';
ytVideoApp.TOP_SEARCH_CONTAINER_DIV = 'searchBox';
ytVideoApp.VIDEO_DATA_EDIT_DIV = 'editForm';
ytVideoApp.DOWNLOAD_INFO = 'downloadInfo';
ytVideoApp.OAUTH2_LOGIN = 'oauth2loin';



ytVideoApp.sendRequest = function(filePath, params, resultDivName) {
  if (window.XMLHttpRequest) {
    var xmlhr = new XMLHttpRequest();
  } else {
    var xmlhr = new ActiveXObject('MSXML2.XMLHTTP.3.0');
  }
  xmlhr.open('POST', filePath);
  xmlhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xmlhr.onreadystatechange = function() {
    var resultDiv = document.getElementById(resultDivName);
    if (xmlhr.readyState == 1) {
      resultDiv.innerHTML = '<b>Loading...</b>';
    } else if (xmlhr.readyState == 4 && xmlhr.status == 200) {
      if (xmlhr.responseText) {
        resultDiv.innerHTML = xmlhr.responseText;
        
      }
    } else if (xmlhr.readyState == 4) {
      alert('Invalid response received - Status: ' + xmlhr.status);
    }
  }
  xmlhr.send(params);
  setTimeout('layoutGrid();',3000);
}

ytVideoApp.hideNavigation = function() {

};

ytVideoApp.refreshSearchResults = function() {
  document.getElementById(ytVideoApp.VIDEO_SEARCH_RESULTS_DIV).innerHTML = '';
}
ytVideoApp.retrievePlaylists = function() {
  var filePath = '/Index/operations';
  var params = 'operation=retrieve_playlists';
  ytVideoApp.sendRequest(filePath, params, ytVideoApp.VIDEO_LIST_CONTAINER_DIV);
}

ytVideoApp.viewPlayListGrid = function (feed_url,pageToken){

    var filePath = '/Index/operations';
    feed_url=document.getElementById('select-choice-1').options[document.getElementById('select-choice-1').selectedIndex].value;
    var params = 'operation=feeds_from_playlist&feed_url='+feed_url;

    if(pageToken!=null){
        params= params + '&pageToken=' + pageToken;
    }
    document.getElementById('container').innerHTML='';
    ytVideoApp.sendRequest(filePath, params, ytVideoApp.VIDEO_GRID_DIV);
    ytVideoApp.feed_to_textarea(feed_url);
    setTimeout('window.scroll(0,0)',3000);
    
    if( (navigator.userAgent.indexOf('iPhone') > 0) || (navigator.userAgent.indexOf('Android') > 0)){
         $("body").css("zoom","100%");
        
    }
    
};

ytVideoApp.feeds_from_keyword= function(keyword,pageToken){

    var filePath = '/Index/operations';
    var params = 'operation=feeds_from_keyword&keyword='+keyword;
    if(pageToken!=null){
        params= params + '&pageToken=' + pageToken;
    }
    document.getElementById('container').innerHTML='';
    ytVideoApp.sendRequest(filePath, params, ytVideoApp.VIDEO_GRID_DIV);
    ytVideoApp.feed_to_textarea_keyword(keyword);
    setTimeout('window.scroll(0,0)',3000);

};
ytVideoApp.showDownLoadLink = function (videoid){
  var filePath = '/getvideo.php?';
  var params = 'videoid='+videoid+'&type=' + 'Download';
  document.getElementById(ytVideoApp.DOWNLOAD_INFO).innerHTML="";
  ytVideoApp.sendRequest(filePath, params, ytVideoApp.DOWNLOAD_INFO);
};

ytVideoApp.shuffle = function (ytplayer){

    var video_ids_csv = document.f2.csv_video_ids.value;
    var ids =video_ids_csv.split(',');
    var index = Math.floor(Math.random()*(ids.length));
    ytplayer.loadVideoById((ids[index]));
};

ytVideoApp.feed_to_textarea = function (feed_url){

    var filePath = '/Index/operations';
    var params = 'operation=teaxtarea_from_palylist&feed_url='+feed_url;

    document.getElementById(ytVideoApp.VIDEO_TEXTAREA_DIV).innerHTML="";
    ytVideoApp.sendRequest(filePath, params, ytVideoApp.VIDEO_TEXTAREA_DIV);
    setTimeout("(function() {   var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;  po.src = 'https://apis.google.com/js/plusone.js';   var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);   })();",2000);

};

ytVideoApp.feed_to_textarea_keyword = function (keyword){

    var filePath = '/Index/operations';
    var params = 'operation=teaxtarea_from_keyword&keyword='+keyword;

    document.getElementById(ytVideoApp.VIDEO_TEXTAREA_DIV).innerHTML='';
    ytVideoApp.sendRequest(filePath, params, ytVideoApp.VIDEO_TEXTAREA_DIV);
    setTimeout("(function() {   var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;  po.src = 'https://apis.google.com/js/plusone.js';   var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);   })();",2000);

};


ytVideoApp.logout = function() {

    location.href="/Index/logout";

};

ytVideoApp.oauth2login = function() {

    var filePath = '/Index/operations';
    var params = 'operation=oauth2login'+ '&nextUrl=/Index/operations';

    ytVideoApp.sendRequest(filePath, params, ytVideoApp.AUTHSUB_REQUEST_DIV);

};

